package com.smcdd.cdb.dbo.cdb_dbo.ui.widget;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;


import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Address;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.AssetsAnalysis;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.JsonUtil;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Arthas on 2017-06-01.
 * ---我的儿子，当你出生的那一天，整个洛丹伦的森林都在低语着同一个名字，阿尔萨斯……---
 * 邮箱:manhuizai@163.com
 * <p>
 * 报价页面选择报价地址popup
 */

public class QuotaAddressPopup {
    private static final String TAG = "QuotaAddressPopup";
    private int quotaType;

    private String code; //地区code码;

    private View contentView;
    private PopupWindow addrPopWindow;
    private LinearLayout boxBtnCancel;
    private LinearLayout boxBtnOk;
    protected boolean isDataLoaded = false;
    /**
     * 选择地址
     */
    private View rootview;
    private boolean isAddrChoosed = false;

    private Activity context;
    private AddressListener addressListener;
    private Address testBean;

    public QuotaAddressPopup(Activity context, View rootview, AddressListener addressListener) {
        super();
        this.context = context;
        this.addressListener = addressListener;
        this.rootview = rootview;
       // this.quotaType = quotaType;
        initProviceSelectView();
    }

    public void showAddressPopup() {
        addrPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);
        ScreenUtils.backgroundAlpha(0.5f, context);
    }

    private void initProviceSelectView() {
        contentView = LayoutInflater.from(context).inflate(
                R.layout.layout_wheelview_site, null);
        mProvincePicker = (WheelView) contentView.findViewById(R.id.wl_province);
        mCityPicker = (WheelView) contentView.findViewById(R.id.wl_city);
        mCountyPicker = (WheelView) contentView.findViewById(R.id.wl_county);
        boxBtnCancel = (LinearLayout) contentView.findViewById(R.id.box_btn_cancel);
        boxBtnOk = (LinearLayout) contentView.findViewById(R.id.box_btn_ok);

        addrPopWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
//        addrPopWindow.setBackgroundDrawable(new ColorDrawable(0xffffff));
        addrPopWindow.setBackgroundDrawable(new BitmapDrawable());
        addrPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                ScreenUtils.backgroundAlpha(1f, context);
            }
        });
        addrPopWindow.setAnimationStyle(R.style.buttom_popup);
        mProvincePicker.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                String provinceText = mProvinceDatas.get(id);
                String provinceCode = mProvinceCodes.get(id);
                String provinceid = mProvinceIds.get(id);
                if (!mCurrentProviceName.equals(provinceText)) {
                    mCurrentProviceName = provinceText;
                    mCurrentProviceCode = provinceCode;
                    mCurrentProviceId = provinceid;
                    ArrayList<String> mCityData = mCitisDatasMap.get(mCurrentProviceName);
                    ArrayList<String> mCityCode = mCitisCodesMap.get(mCurrentProviceCode);
                    ArrayList<String> mCityId = mCitisCodesMap.get(mCurrentProviceId);
                    mCityPicker.resetData(mCityData);
                    mCityPicker.setDefault(0);
                    mCurrentCityName = mCityData.get(0);
                    mCurrentCityCode = mCityCode.get(0);
                    mCurrentCityId = mCityId.get(0);

                    ArrayList<String> mDistrictData = mDistrictDatasMap.get(mCurrentCityName);
                    ArrayList<String> mDistrictCode = mDistrictCodesMap.get(mCurrentCityCode);
                    ArrayList<String> mDistrictId = mDistrictIdsMap.get(mCurrentCityId);
                    mCountyPicker.resetData(mDistrictData);
                    mCountyPicker.setDefault(0);
                    mCurrentDistrictName = mDistrictData.get(0);
                    mCurrentDistrictCode = mDistrictCode.get(0);
                    mCurrentDistrictId = mDistrictId.get(0);
                }
            }

            @Override
            public void selecting(int id, String text) {
            }
        });

        mCityPicker.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                ArrayList<String> mCityData = mCitisDatasMap.get(mCurrentProviceName);
                ArrayList<String> mCityCode = mCitisCodesMap.get(mCurrentProviceCode);
                ArrayList<String> mCityId = mCitisCodesMap.get(mCurrentProviceId);
                String cityText = mCityData.get(id);
                String cityCode = mCityCode.get(id);
                String cityId = mCityId.get(id);
                if (!mCurrentCityName.equals(cityText)) {
                    mCurrentCityName = cityText;
                    mCurrentCityCode = cityCode;
                    mCurrentCityId = cityId;
                    ArrayList<String> mCountyData = mDistrictDatasMap.get(mCurrentCityName);
                    ArrayList<String> mCountyCode = mDistrictCodesMap.get(mCurrentCityCode);
                    ArrayList<String> mCountyId = mDistrictIdsMap.get(mCurrentCityId);
                    mCountyPicker.resetData(mCountyData);
                    mCountyPicker.setDefault(0);
                    mCurrentDistrictName = mCountyData.get(0);
                    mCurrentDistrictCode = mCountyCode.get(0);
                    mCurrentDistrictId = mCountyId.get(0);
                }
            }

            @Override
            public void selecting(int id, String text) {

            }
        });

        mCountyPicker.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                ArrayList<String> mDistrictData = mDistrictDatasMap.get(mCurrentCityName);
                ArrayList<String> mDistrictCode = mDistrictCodesMap.get(mCurrentCityCode);
                ArrayList<String> mDistrictId = mDistrictIdsMap.get(mCurrentCityId);
                String districtText = mDistrictData.get(id);
                String districtCode = mDistrictCode.get(id);
                String districtId = mDistrictId.get(id);
                if (!mCurrentDistrictName.equals(districtText)) {
                    mCurrentDistrictName = districtText;
                    mCurrentDistrictCode = districtCode;
                    mCurrentDistrictId = districtId;
                }
            }

            @Override
            public void selecting(int id, String text) {

            }
        });

        boxBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addrPopWindow.dismiss();
            }
        });

        boxBtnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isAddrChoosed = true;
                StringBuffer address = new StringBuffer();
                if (TextUtils.isEmpty(mCurrentDistrictName)) {
                    address.append(mCurrentProviceName + mCurrentCityName);
                    code = mCurrentCityCode;
                    String name = mCurrentProviceName+"-"+mCurrentCityName+"-"+mCurrentDistrictName;
                    addrPopWindow.dismiss();
                    addressListener.onSelectOK(name,mCurrentProviceName,mCurrentCityName,mCurrentDistrictName,mCurrentProviceId,mCurrentCityId,mCurrentDistrictId);
                   // addressListener.onSelectOK(mCurrentProviceName+mCurrentProviceId+mCurrentCityName+mCurrentCityId+mCurrentDistrictName+mCurrentDistrictId);
                } else {
                    address.append(mCurrentProviceName + mCurrentCityName + mCurrentDistrictName);
                    code = mCurrentDistrictCode;
                    //addressListener.onSelectOK(mCurrentProviceName+mCurrentProviceId+mCurrentCityName+mCurrentCityId+mCurrentDistrictName+mCurrentDistrictId);
                    String name = mCurrentProviceName+"-"+mCurrentCityName+"-"+mCurrentDistrictName;
                    addrPopWindow.dismiss();
                    addressListener.onSelectOK(name,mCurrentProviceName,mCurrentCityName,mCurrentDistrictName,mCurrentProviceId,mCurrentCityId,mCurrentDistrictId);
                }
                addrPopWindow.dismiss();
            }
        });
        //加载地址信息
        disposeAddress();
        // 启动线程读取数据
    }


    /**
     * 所有省相关数据
     */
    protected ArrayList<String> mProvinceDatas;
    protected ArrayList<String> mProvinceCodes;
    protected ArrayList<String> mProvinceIds;
    /**
     * 所有市相关数据
     */
    protected Map<String, ArrayList<String>> mCitisDatasMap = new HashMap<>();
    protected Map<String, ArrayList<String>> mCitisCodesMap = new HashMap<>();
    protected Map<String, ArrayList<String>> mCitisIdsMap = new HashMap<>();
    /**
     * 所有地区相关数据
     */
    protected Map<String, ArrayList<String>> mDistrictDatasMap = new HashMap<>();
    protected Map<String, ArrayList<String>> mDistrictCodesMap = new HashMap<>();
    protected Map<String, ArrayList<String>> mDistrictIdsMap = new HashMap<>();
    //当前显示的省名称和code
    protected String mCurrentProviceName;
    protected String mCurrentProviceCode;
    protected String mCurrentProviceId;
    //当前显示的市名称和code
    protected String mCurrentCityName;
    protected String mCurrentCityCode;
    protected String mCurrentCityId;
    //当前显示的地区名称和code
    protected String mCurrentDistrictName;
    protected String mCurrentDistrictCode;
    protected String mCurrentDistrictId;

    public void disposeAddress() {
        //获取raw下的json文件并解析
        String json = AssetsAnalysis.getData(context.getResources().openRawResource(R.raw.address));
        if (json != null) {
            testBean = JsonUtil.parseJsonToBean(json, Address.class);
        }
        mProvinceDatas = new ArrayList<>();
        mProvinceCodes = new ArrayList<>();
        mProvinceIds = new ArrayList<>();

        ArrayList<String> cityNames = null;     //省对应的所有市的名称集合
        ArrayList<String> cityCodes = null;     //省对应的所有市的code集合
        ArrayList<String> cityIds = null;     //省对应的所有市的id集合
        /**
         * 获取省数据
         */
        for (Address.DataBean dataBean : testBean.getData()) {
            if ("0".equals(dataBean.getPid())) {
                mProvinceDatas.add(dataBean.getName());
                mProvinceCodes.add(dataBean.getCode());
                mProvinceIds.add(dataBean.getId());
            }
        }
        mCurrentProviceName = mProvinceDatas.get(0);
        mCurrentProviceCode = mProvinceCodes.get(0);
        //新家的ID
        mCurrentProviceId=mProvinceIds.get(0);

        /**
         * 获取市数据
         */
        for (int i = 0; i < mProvinceDatas.size(); i++) {       //遍历每个省
            cityNames = new ArrayList<>();
            cityCodes = new ArrayList<>();
            cityIds = new ArrayList<>();
            for (Address.DataBean dataBean : testBean.getData()) {     //遍历所有数据,找到当前省名下的所有市的数据
                if (dataBean.getPid().equals(mProvinceIds.get(i))) {    //当前item的pid等于当前遍历省的id时,往市集合中添加数据
                    cityNames.add(dataBean.getName());
                    cityCodes.add(dataBean.getCode());
                    cityIds.add(dataBean.getId());
                }
            }
            mCitisDatasMap.put(mProvinceDatas.get(i), cityNames);       //往市map中  添加对应的省的名称和市的名称数据集合
            mCitisCodesMap.put(mProvinceCodes.get(i), cityCodes);       //往市map中  添加对应的省的名称和市的Code数数据集合
            mCitisIdsMap.put(mProvinceIds.get(i), cityIds);             //往市map中  添加对应的省的名称和市的Id数数据集合
        }
        mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName).get(0);
        mCurrentCityCode = mCitisCodesMap.get(mCurrentProviceCode).get(0);
        mCurrentCityId = mCitisIdsMap.get(mCurrentProviceId).get(0);
        /**
         * 获取地区数据
         */
        ArrayList<String> distrinctNameArray = null;    //市对应的所有地区名称的集合
        ArrayList<String> distrinctCodeArray = null;//市对应的所有地区code的集合
        ArrayList<String> distrinctIdArray = null;//市对应的所有地区id的集合
        for (int i = 0; i < mProvinceIds.size(); i++) { //遍历省
            ArrayList<String> strings1 = mCitisIdsMap.get(mProvinceIds.get(i));  //拿到省对应的市的ID集合
            ArrayList<String> strings2 = mCitisDatasMap.get(mProvinceDatas.get(i));//拿到省对应的市的名称集合
            ArrayList<String> strings3 = mCitisCodesMap.get(mProvinceCodes.get(i)); //拿到省对应的市的code集合

            //获取地区信息
            for (int j = 0; j < strings1.size(); j++) {          //遍历当前省中的所有市
                distrinctNameArray = new ArrayList<>();
                distrinctCodeArray = new ArrayList<>();
                distrinctIdArray = new ArrayList<>();
                for (Address.DataBean dataBean : testBean.getData()) {         //遍历所有数据,找到当前市对应的地区
                    if (dataBean.getPid().equals(strings1.get(j))) {            //如果当前item的pid对应当前市的id  添加数据
                        distrinctNameArray.add(dataBean.getName());
                        distrinctCodeArray.add(dataBean.getCode());
                        distrinctIdArray.add(dataBean.getId());
                    }
                }
                mDistrictDatasMap.put(strings2.get(j), distrinctNameArray);  //往地区map中  添加该市名称和对应的地区的名称数据集合
                mDistrictCodesMap.put(strings3.get(j), distrinctCodeArray);    //往地区map中  添加该市code和市对应的地区的code数据集合
                //  distrinctIdArray   地区不需要id了
                mDistrictIdsMap.put(strings1.get(j),distrinctIdArray);
            }
        }
        mCurrentDistrictName=mDistrictDatasMap.get(mCurrentCityName).get(0);
        mCurrentDistrictCode=mDistrictCodesMap.get(mCurrentCityCode).get(0);
        mCurrentDistrictId=mDistrictIdsMap.get(mCurrentCityId).get(0);
        setAddress();
    }

    //往wheelView中设置数据
    WheelView mProvincePicker;
    WheelView mCityPicker;
    WheelView mCountyPicker;
    public void setAddress(){
        //设置省
        mProvincePicker.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                String provinceText = mProvinceDatas.get(id);
                String provinceCode = mProvinceCodes.get(id);
                String provinceId = mProvinceIds.get(id);
                if (!mCurrentProviceName.equals(provinceText)) {
                    mCurrentProviceName = provinceText;
                    mCurrentProviceCode = provinceCode;
                    mCurrentProviceId = provinceId;
                    ArrayList<String> mCityData = mCitisDatasMap.get(mCurrentProviceName);
                    ArrayList<String> mCityCode = mCitisCodesMap.get(mCurrentProviceCode);
                    ArrayList<String> mCityId = mCitisIdsMap.get(mCurrentProviceId);
                    mCityPicker.resetData(mCityData);
                    mCityPicker.setDefault(0);
                    mCurrentCityName = mCityData.get(0);
                    mCurrentCityCode = mCityCode.get(0);
                    mCurrentCityId = mCityId.get(0);

                    ArrayList<String> mDistrictData = mDistrictDatasMap.get(mCurrentCityName);
                    ArrayList<String> mDistrictCode = mDistrictCodesMap.get(mCurrentCityCode);
                    ArrayList<String> mDistrictId = mDistrictIdsMap.get(mCurrentCityId);
                    mCountyPicker.resetData(mDistrictData);
                    mCountyPicker.setDefault(0);
                    mCurrentDistrictName = mDistrictData.get(0);
                    mCurrentDistrictCode = mDistrictCode.get(0);
                    mCurrentDistrictId = mDistrictId.get(0);
                }
            }

            @Override
            public void selecting(int id, String text) {
            }
        });
        //设置市
        mCityPicker.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                ArrayList<String> mCityData = mCitisDatasMap.get(mCurrentProviceName);
                ArrayList<String> mCityCode = mCitisCodesMap.get(mCurrentProviceCode);
                ArrayList<String> mCityId = mCitisIdsMap.get(mCurrentProviceId);
                String cityText = mCityData.get(id);
                String cityCode = mCityCode.get(id);
                String cityId = mCityId.get(id);
                if (!mCurrentCityName.equals(cityText)) {
                    mCurrentCityName = cityText;
                    mCurrentCityCode = cityCode;
                    mCurrentCityId = cityId;
                    ArrayList<String> mCountyData = mDistrictDatasMap.get(mCurrentCityName);
                    ArrayList<String> mCountyCode = mDistrictCodesMap.get(mCurrentCityCode);
                    ArrayList<String> mCountyId = mDistrictIdsMap.get(mCurrentCityId);
                    mCountyPicker.resetData(mCountyData);
                    mCountyPicker.setDefault(0);
                    mCurrentDistrictName = mCountyData.get(0);
                    mCurrentDistrictCode = mCountyCode.get(0);
                    mCurrentDistrictId = mCountyId.get(0);
                }
            }

            @Override
            public void selecting(int id, String text) {

            }
        });

        //设置地区
        mCountyPicker.setOnSelectListener(new WheelView.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
                ArrayList<String> mDistrictData = mDistrictDatasMap.get(mCurrentCityName);
                ArrayList<String> mDistrictCode = mDistrictCodesMap.get(mCurrentCityCode);
                ArrayList<String> mDistrictId = mDistrictIdsMap.get(mCurrentCityId);
                Log.e("a","aaaaa"+id);
                try{


                String districtText = mDistrictData.get(id);
                String districtCode = mDistrictCode.get(id);
                String districtId = mDistrictId.get(id);

                if (!mCurrentDistrictName.equals(districtText)) {
                    mCurrentDistrictName = districtText;
                    mCurrentDistrictCode = districtCode;
                    mCurrentDistrictId = districtId;
                }
                }catch (Exception e){
                    Log.e("a","aaaaa");
                }
            }

            @Override
            public void selecting(int id, String text) {

            }
        });
        //设置省数据
        mProvincePicker.setData(mProvinceDatas);
        mProvincePicker.setDefault(0);
        //设置市数据
        mCityPicker.setData(mCitisDatasMap.get(mCurrentProviceName));
        mCityPicker.setDefault(0);
        //设置默认地区数据
        mCountyPicker.setData(mDistrictDatasMap.get(mCurrentCityName));
        mCountyPicker.setDefault(0);

    }

//    //获取当前选择省市县
//    public interface getAddressStr(){
//        void onSelectOK(String addressStr);
//       // return mCurrentProviceName+mCurrentCityName+mCurrentDistrictName;
//    }

    public interface AddressListener {
        void onSelectOK(String addressStr,String Province,String City,String County,String ProvinceId,String CityId,String CountyId);
    }
}
